//
//  LeftViewController.m
//  BeautyClother
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "LeftViewController.h"
#import "LeftTableViewCell.h"
#import "AsyncConnection.h"
#import "define.h"
#import "LeftDetailViewController.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) LeftTableViewCell *cell;

@end

@implementation LeftViewController


- (void)dealloc
{
    [_table release];
    [_array release];
    [_cell release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [ UIColor whiteColor];
    [self conntction];
    [self createHotTable];
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
//    self.title = @"lalalla";
    // Do any additional setup after loading the view.
}
- (void)back:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)conntction
{
    NSDictionary *dic = @{@"page":@"1",
                          @"temp":@"255",
                          @"token":@"1416b90f03d637a168f695105aa4132d7",
                          @"col":self.col,
                          @"ua":@"iPhone%7C1.7.5",
                          @"lang":@"en",
                          @"sig":@"f26215b241f41441632303067",
                          @"nt":@"wifi"
                          
                          };
   // ?page=1&temp=255&token=1416b90f03d637a168f695105aa4132d7&col=4&ua=iPhone%7C1.7.5&lang=en&sig=f26215b241f41441632303067&nt=wifi
    [AsyncConnection asyncConnection:LEFT parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [dic objectForKey:@"colls"];
        
        [self.table reloadData];
    } path:[NSString stringWithFormat:@"%@%@", @"comm_left", self.col]];
}

- (void)createHotTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"rrr"];
    if (_cell == nil) {
        _cell = [[LeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rrr"];
    }
    [_cell sendPhoto:[[self.array objectAtIndex:indexPath.row]objectForKey:@"items"]];
    _cell.title.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"desc"];
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return _cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 360;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeftDetailViewController *detail = [[LeftDetailViewController alloc]init];
    detail.lid = [[self.array objectAtIndex:indexPath.row]objectForKey:@"ruleId"];
    NSArray *arr = [[self.array objectAtIndex:indexPath.row]objectForKey:@"items"];
    detail.arr = (NSMutableArray *)arr;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
